﻿function getObjects(obj, key, val) {
    var objects = [];
    for (var i in obj) {
        if (!obj.hasOwnProperty(i)) continue;
        if (typeof obj[i] == 'object') {
            objects = objects.concat(getObjects(obj[i], key, val));
        } else if (i == key && obj[key] == val) {
            objects.push(obj);
        }
    }
    return objects;
}


function BuildOptionForBarChart(data, option) {
    var dataSet = data;
    option.legend.data = [];
    option.series = [];

    if (dataSet != null && dataSet.length > 0) {
        var legendData = option.legend.data;
        var xAxisData = option.xAxis[0].data;
        var series = option.series;

        for (var i = 0 ; i < dataSet.length; i++) {
            var currentData = dataSet[i];
            for (var j = 0; j < currentData.SeriesDataSet.length; j++) {
                currentSeriesData = currentData.SeriesDataSet[j];
                var legendName = currentSeriesData.Name;

                //legend
                if (legendData == null || legendData.length == 0 || legendData.indexOf(legendName) < 0) {
                    legendData.push(legendName)
                }

                //series
                var currentSeries = getObjects(series, 'name', currentSeriesData.Name);
                if (currentSeries == null || currentSeries[0] == null) {
                    currentSeries = {
                        name: currentSeriesData.Name,
                        type: 'bar',
                        data: [currentSeriesData.Value],
                        itemStyle: { normal: { label: { show: true } } },
                    };
                    //if (dataSet.length > 1)
                    //{
                    //    currentSeries.stack='Total';
                    //}
                    series.push(currentSeries);
                }
                else {
                    currentSeries[0].data.push(currentSeriesData.Value);
                }
            }

            //xAxis
            xAxisData.push(currentData.XAxisData);
        }
    }

    return option;
}

function BuildOptionForPieChart(data, option) {
    var dataSet = data;
    option.legend.data = [];
    option.series = [];
    if (dataSet != null && dataSet.length > 0) {
        var legendData = option.legend.data;
        var series = option.series;

        for (var i = 0 ; i < dataSet.length; i++) {
            var currentData = dataSet[i];
            var currentSeries = {
                name: currentData.SeriesName,
                type: 'pie',
                radius: eval(currentData.Radius),
                data: [],
                itemStyle: { normal: { label: { show: true, position: currentData.LabelPosition } } },
            };

            for (var j = 0; j < currentData.SeriesDataSet.length; j++) {
                var currentSeriesData = currentData.SeriesDataSet[j];
                currentSeries.data.push({ name: currentSeriesData.Name, value: currentSeriesData.Value });

                var legendName = currentSeriesData.Name;
                //legend
                if (legendData.indexOf(legendName) < 0) {
                    legendData.push(legendName)
                }
            }

            series.push(currentSeries);
            
        }
    }
    return option;
};